package com.opt.sample;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelManager {

	private static Logger LOGGER = Logger.getLogger(ExcelManager.class);

	private String COLUMN_PROFIT_NAME = "ZYSK";

	private String COLUMN_CITY_NAME = "MIASTO";
	private String COLUMN_PRIORITY_NAME = "PRIORYTET";

	private String COLUMN_ITERATION_NAME = "ITERACJA";
	private String COLUMN_GOAL_FUNCTION_NAME = "FUNKCJA_CELU";

	private int COLUMN_PROFIT;
	private int COLUMN_CITY;
	private int COLUMN_PRIORITY;

	private int COLUMN_ITERATION;
	private int COLUMN_GOAL_FUNCTION;

	private HSSFWorkbook workbook = null;
	private HSSFSheet WORKSHEET;

	private FileInputStream inputStream = null;
	private FileOutputStream outputStream = null;

	public ExcelManager(String filepath) {
		this.WORKSHEET = initFile(filepath);
	}

	private HSSFSheet initFile(String filepath) {
		// FileInputStream inputStream = null;
		// HSSFWorkbook workbook = null;
		HSSFSheet hssfSheet = null;
		try {
			this.inputStream = new FileInputStream(filepath);
			this.workbook = new HSSFWorkbook(inputStream);
			hssfSheet = workbook.getSheet("Arkusz1");
			// this.outputStream = new FileOutputStream(filepath);
		} catch (FileNotFoundException e) {
			LOGGER.error(e);
		} catch (IOException e) {
			LOGGER.error(e);
		}

		return hssfSheet;

	}

	private void initColumnNumbers(HSSFRow headerRow) {
		int cellsNumber = headerRow.getLastCellNum();
		for (int i = 0; i < cellsNumber; i++) {
			HSSFCell cell = headerRow.getCell(i);
			if (COLUMN_PROFIT_NAME.equals(cell.getStringCellValue()))
				COLUMN_PROFIT = cell.getColumnIndex();
			if (COLUMN_CITY_NAME.equals(cell.getStringCellValue()))
				COLUMN_CITY = cell.getColumnIndex();
			if (COLUMN_PRIORITY_NAME.equals(cell.getStringCellValue()))
				COLUMN_PRIORITY = cell.getColumnIndex();
			if (COLUMN_ITERATION_NAME.equals(cell.getStringCellValue()))
				COLUMN_ITERATION = cell.getColumnIndex();
			if (COLUMN_GOAL_FUNCTION_NAME.equals(cell.getStringCellValue()))
				COLUMN_GOAL_FUNCTION = cell.getColumnIndex();

		}
	}

	public NewPackage getCandidate() {

		initColumnNumbers(WORKSHEET.getRow(0));

		int rowNumber = WORKSHEET.getLastRowNum();
		// System.out.println(rowNumber);

		Random rand = new Random();
		int randInt = rand.nextInt(rowNumber + 1);

		while (randInt == 0) {
			randInt = rand.nextInt(rowNumber + 1);
		}

		HSSFRow row = WORKSHEET.getRow(randInt);

		HSSFCell cellProfit = row.getCell(COLUMN_PROFIT);
		HSSFCell cellCity = row.getCell(COLUMN_CITY);
		HSSFCell cellPriority = row.getCell(COLUMN_PRIORITY);

		NewPackage candidate = new NewPackage();

		candidate.setCandCity(cellCity.getNumericCellValue());

		candidate.setCandPriority(cellPriority.getNumericCellValue());

		candidate.setCandProfit(cellProfit.getNumericCellValue());

		//

		return candidate;
	}

	public void setValues(int iteration, double goalFunction, String filepath) {

		HSSFRow row = WORKSHEET.getRow(iteration);

		HSSFCell cellIteration;
		HSSFCell cellGoalFunction;

		cellIteration = row.createCell(COLUMN_ITERATION);
		cellGoalFunction = row.createCell(COLUMN_GOAL_FUNCTION);

		cellIteration.setCellValue(iteration);
		cellGoalFunction.setCellValue(goalFunction);

		try {
			this.outputStream = new FileOutputStream(filepath);
			this.workbook.write(outputStream);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closeFile() {
		try {
			this.inputStream.close();
			this.outputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
